import unittest
import os
import jsonpath
from unittestreport import ddt, list_data
from Common.common_test import CommonTest
from Common.handle_excel import HandleExcel
from Common.handle_path import datas_dir
from Common.my_logger import logger
from Common.handle_request import send_requests
from Common.handle_config import conf
from Common.handle_data import EnvData, replace_case_by_regular, clear_EnvData_attrs

# 获取测试数据
he = HandleExcel(os.path.join(datas_dir, 'api_test_data.xlsx'), 'Save2004')
cases = he.read_all_datas()
he.close_file()

com_test = CommonTest()

# @ddt
# class TestSave2004(unittest.TestCase):
#     @classmethod
#     def setUpClass(cls) -> None:
#         logger.info('*************************************新建项目测试开始*********************************************')
#         clear_EnvData_attrs()  # 清除本次测试添加的环境变量
#         com_test.Login()
#
#     @classmethod
#     def tearDownClass(cls) -> None:
#         logger.info('*************************************新建项目测试结束*********************************************')
#
#     @list_data(cases)
#     def test_PCListV2(self, case):
#         logger.info('***********第{}条用例，用例title是：{} ***********'.format(case['id'], case['title']))
#         replace_case_by_regular(case)
#         comment = send_requests(case['method'], case['url'], case['data'], case['headers'])
#         expected = eval(case['expected'])
#         id_row = case['id']
#
#         try:
#             self.assertEqual(comment.json()['code'], expected['code'])
#             self.assertEqual(comment.json()['message'], expected['message'])
#         except AssertionError as a:
#             # he.write_data(row=id_row, column=8, value='失败')
#             raise a
#         else:
#             logger.info('用例执行通过')
#             # he.write_data(row=id_row, column=8, value='PASS')
#
#         com_test.Delete_Exam()

# 获取测试数据
he = HandleExcel(os.path.join(datas_dir, 'api_test_data.xlsx'), 'CutType')
CutType = he.read_all_datas()
he.close_file()

@ddt
class Test_CutType(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        clear_EnvData_attrs()
        com_test.Save2004()
        com_test.Get_Exam()
        logger.info('**************************修改项目属性测试开始********************************')

    @classmethod
    def tearDownClass(cls) -> None:
        # com_test.Delete_Exam()
        logger.info('**************************修改项目属性测试结束********************************')

    @list_data(CutType)
    def test_UpdateExamTyp(self,case):
        case = replace_case_by_regular(case)
        logger.info('***********第{}条用例，用例title是：{} ***********'.format(case['id'], case['title']))
        res = send_requests(case['method'],case['url'],case['data'],case['headers'])
        print(res.json())
        expected = eval(case['expected'])
        try:
            self.assertEqual(res.json()['code'],expected['code'])
            self.assertEqual(res.json()['message'],expected['message'])
        except ArithmeticError as a:
            raise a
        else:
            logger.info('用例执行通过')




if __name__ == '__main__':
    unittest.main()
